#include "sdk_project_config.h"
#include "app_exv.h"

/*控制周期,Exv_Cycle单位为ms*/
int Exv_Cycle;

/*计时器，根据周期来，每一个周期进入一次引脚变化*/
int Exv_Time_Count;

/*引脚指示，根据阀体设计拉高/低引脚*/
int Four_phase_Count;

/*开启指示，1为开始，0为关闭*/
uint8_t Four_Phase_Enable;

/*正反转指示，1为正转，0为反转*/
uint8_t Wise_Flag;

/*测试用定义区*/
#if 0
volatile int Test_Flag1;
volatile int Test_Flag2;
volatile int Test_Flag3;
volatile int Test_Flag4;
#endif

/*初始化*/
void app_exvInit(void)
{
	Exv_Time_Count = 0;
	Four_Phase_Enable = 1;/**/
	Wise_Flag = 1;				/*初始转动方向，1为顺时针转动，0为逆时针转动*/
	Four_phase_Count = 0;	/*周期递增，功能函数放在1MStask当中，因此1MS前进一次*/
	Exv_Cycle = 16;				/*周期，单位MS*/
}

/*
A  --- 2 --- 129 --- C5 --- 11000001 --- 11100011
B  --- 1 --- 126 --- E1 --- 01110000 --- 11111000
A/ --- 4 --- 128 --- E0 --- 00011100 --- 00111110
B/ --- 3 --- 125 --- E7 --- 00000111 --- 10001111

正 --- C5
负 --- E0

1101
1100
1110
0110
0111
0011
1011
1001
*/
void Set_4Pins(int Pin1,int Pin2,int Pin3,int Pin4)
{
	PINS_DRV_WritePin(GPIOC,5,Pin1);
	PINS_DRV_WritePin(GPIOE,1,Pin2);
	PINS_DRV_WritePin(GPIOE,0,Pin3);
	PINS_DRV_WritePin(GPIOE,7,Pin4);
}
void Phase_Driver(int Phase)
{   
	switch (Phase)
	{
		case 0: 
			Set_4Pins(1,1,0,1);//1101
			break;
		case 1:	
			Set_4Pins(1,1,0,0);//1100
			break;
		case 2:	
			Set_4Pins(1,1,1,0);//1110
			break;
		case 3:	
			Set_4Pins(0,1,1,0);//0110
			break;
		case 4:
			Set_4Pins(0,1,1,1);//0111
			break;
		case 5:
			Set_4Pins(0,0,1,1);//0011
			break;
		case 6:
			Set_4Pins(1,0,1,1);//1011
			break;
		case 7:
			Set_4Pins(1,0,0,1);//1001
			break;
		case 8:
			Set_4Pins(1,1,1,1);//全部关闭
			break;
		default: break ;
	}
}
/*
四相发波控制器
膨胀阀控制手册提供最小单元格为8，因此周期尽可能选用8的倍数
正相序输出正转，逆相序输出反转
具体参考手册
直接将此函数输入Task当中即可
*/
void Four_Phase_OutPut(void)
{
	if(Exv_Time_Count == 0)	//每Exv_Cycle时间进入一次
	{
		if(Four_Phase_Enable)	//使能转动，否则停机
		{
			if(Wise_Flag)				//判断正反转
			{
				Phase_Driver(Four_phase_Count);
			}
//			else
//			{
//				Phase_Driver(7 - Four_phase_Count);
//			}
			Four_phase_Count ++;
			if (Four_phase_Count > 7) Four_phase_Count = 0;
		}
		else
		{
			Phase_Driver(8);		//关闭信号
		}
	}
	Exv_Time_Count ++;
	if(Exv_Time_Count >= 25) Exv_Time_Count = 0;
}
/*
输入相序和状态，执行引脚拉高/低。
Phase：从0~3依次是E7,E1,E0,C5
State：引脚拉高拉低
*/
/*
void Phase_Driver(int Phase)
{   
	switch (Phase)
	{
		case 0: 
			PINS_DRV_WritePin(GPIOE,7,1);
			PINS_DRV_WritePin(GPIOE,1,0);
			PINS_DRV_WritePin(GPIOE,0,0);
			PINS_DRV_WritePin(GPIOC,5,0);
			break;
		case 1:	
			PINS_DRV_WritePin(GPIOE,7,1);
			PINS_DRV_WritePin(GPIOE,1,1);
			PINS_DRV_WritePin(GPIOE,0,0);
			PINS_DRV_WritePin(GPIOC,5,0);
			break;
		case 2:	
			PINS_DRV_WritePin(GPIOE,7,0);
			PINS_DRV_WritePin(GPIOE,1,1);
			PINS_DRV_WritePin(GPIOE,0,0);
			PINS_DRV_WritePin(GPIOC,5,0);
			break;
		case 3:	
			PINS_DRV_WritePin(GPIOE,7,0);
			PINS_DRV_WritePin(GPIOE,1,1);
			PINS_DRV_WritePin(GPIOE,0,1);
			PINS_DRV_WritePin(GPIOC,5,0);
			break;
		case 4:
			PINS_DRV_WritePin(GPIOE,7,0);
			PINS_DRV_WritePin(GPIOE,1,0);
			PINS_DRV_WritePin(GPIOE,0,1);
			PINS_DRV_WritePin(GPIOC,5,0);
			break;
		case 5:
			PINS_DRV_WritePin(GPIOE,7,0);
			PINS_DRV_WritePin(GPIOE,1,0);
			PINS_DRV_WritePin(GPIOE,0,1);
			PINS_DRV_WritePin(GPIOC,5,1);
			break;
		case 6:
			PINS_DRV_WritePin(GPIOE,7,0);
			PINS_DRV_WritePin(GPIOE,1,0);
			PINS_DRV_WritePin(GPIOE,0,0);
			PINS_DRV_WritePin(GPIOC,5,1);
			break;
		case 7:
			PINS_DRV_WritePin(GPIOE,7,1);
			PINS_DRV_WritePin(GPIOE,1,0);
			PINS_DRV_WritePin(GPIOE,0,0);
			PINS_DRV_WritePin(GPIOC,5,1);
			break;
		case 8:
			PINS_DRV_WritePin(GPIOE,7,0);
			PINS_DRV_WritePin(GPIOE,1,0);
			PINS_DRV_WritePin(GPIOE,0,0);
			PINS_DRV_WritePin(GPIOC,5,0);
			break;
		default: break ;
	}
}
*/